package dcc.component;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import dcc.mbg.model.User;
import dcc.tools.BeanFactory;
import dcc.tools.Tools;

public class JUserManagePanel extends JPanel {

	private JUserTable table;

	public JUserManagePanel() {
		init();
	}

	private void init() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 510, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 0, 400, 0, 0 };
		gridBagLayout.columnWeights = new double[] { 1.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 1.0, 0.0, 1.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);

		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 1;
		add(panel, gbc_panel);
		panel.setLayout(new BorderLayout(0, 0));

		JPanel topPanel = new JPanel();
		topPanel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		topPanel.setPreferredSize(new Dimension(10, 200));
		panel.add(topPanel, BorderLayout.NORTH);
		GridBagLayout gbl_topPanel = new GridBagLayout();
		gbl_topPanel.columnWidths = new int[] { 0, 0, 100, 100, 0, 0 };
		gbl_topPanel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_topPanel.columnWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gbl_topPanel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		topPanel.setLayout(gbl_topPanel);

		JLabel lblUserName = new JLabel("\u7528\u6237\u540D:");
		lblUserName.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		GridBagConstraints gbc_lblUserName = new GridBagConstraints();
		gbc_lblUserName.anchor = GridBagConstraints.EAST;
		gbc_lblUserName.insets = new Insets(0, 0, 5, 5);
		gbc_lblUserName.gridx = 1;
		gbc_lblUserName.gridy = 1;
		topPanel.add(lblUserName, gbc_lblUserName);

		tfUserName = new JTextField();
		GridBagConstraints gbc_tfUserName = new GridBagConstraints();
		gbc_tfUserName.gridwidth = 2;
		gbc_tfUserName.insets = new Insets(0, 0, 5, 5);
		gbc_tfUserName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfUserName.gridx = 2;
		gbc_tfUserName.gridy = 1;
		topPanel.add(tfUserName, gbc_tfUserName);
		tfUserName.setColumns(10);

		JLabel lblPassword = new JLabel("\u5BC6\u3000\u7801:");
		lblPassword.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		GridBagConstraints gbc_lblPassword = new GridBagConstraints();
		gbc_lblPassword.anchor = GridBagConstraints.EAST;
		gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblPassword.gridx = 1;
		gbc_lblPassword.gridy = 2;
		topPanel.add(lblPassword, gbc_lblPassword);

		pfPassword = new JPasswordField();
		GridBagConstraints gbc_pfPassword = new GridBagConstraints();
		gbc_pfPassword.gridwidth = 2;
		gbc_pfPassword.insets = new Insets(0, 0, 5, 5);
		gbc_pfPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_pfPassword.gridx = 2;
		gbc_pfPassword.gridy = 2;
		topPanel.add(pfPassword, gbc_pfPassword);

		JLabel lblOkPassword = new JLabel("\u786E\u8BA4\u5BC6\u7801:");
		lblOkPassword.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		GridBagConstraints gbc_lblOkPassword = new GridBagConstraints();
		gbc_lblOkPassword.anchor = GridBagConstraints.EAST;
		gbc_lblOkPassword.insets = new Insets(0, 0, 5, 5);
		gbc_lblOkPassword.gridx = 1;
		gbc_lblOkPassword.gridy = 3;
		topPanel.add(lblOkPassword, gbc_lblOkPassword);

		pfOkPassword = new JPasswordField();
		GridBagConstraints gbc_pfOkPassword = new GridBagConstraints();
		gbc_pfOkPassword.gridwidth = 2;
		gbc_pfOkPassword.insets = new Insets(0, 0, 5, 5);
		gbc_pfOkPassword.fill = GridBagConstraints.HORIZONTAL;
		gbc_pfOkPassword.gridx = 2;
		gbc_pfOkPassword.gridy = 3;
		topPanel.add(pfOkPassword, gbc_pfOkPassword);

		JLabel lblName = new JLabel("\u771F\u5B9E\u59D3\u540D:");
		lblName.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.anchor = GridBagConstraints.EAST;
		gbc_lblName.insets = new Insets(0, 0, 5, 5);
		gbc_lblName.gridx = 1;
		gbc_lblName.gridy = 4;
		topPanel.add(lblName, gbc_lblName);

		tfName = new JTextField();
		tfName.setColumns(10);
		GridBagConstraints gbc_tfName = new GridBagConstraints();
		gbc_tfName.gridwidth = 2;
		gbc_tfName.insets = new Insets(0, 0, 5, 5);
		gbc_tfName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfName.gridx = 2;
		gbc_tfName.gridy = 4;
		topPanel.add(tfName, gbc_tfName);

		JLabel lblRole = new JLabel("\u89D2\u8272:");
		lblRole.setFont(new Font("Microsoft YaHei", Font.PLAIN, 18));
		GridBagConstraints gbc_lblRole = new GridBagConstraints();
		gbc_lblRole.anchor = GridBagConstraints.EAST;
		gbc_lblRole.insets = new Insets(0, 0, 5, 5);
		gbc_lblRole.gridx = 1;
		gbc_lblRole.gridy = 5;
		topPanel.add(lblRole, gbc_lblRole);

		JButton btnSave = new JButton("\u4FDD\u5B58");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				save();
			}
		});

		JButton button = new JButton("\u6DFB\u52A0");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				add();
			}
		});

		cbRole = new CustomComboBox(CustomComboBox.LoadType.FROMFILE, "ROLE");
		GridBagConstraints gbc_cbRole = new GridBagConstraints();
		gbc_cbRole.gridwidth = 2;
		gbc_cbRole.insets = new Insets(0, 0, 5, 5);
		gbc_cbRole.fill = GridBagConstraints.HORIZONTAL;
		gbc_cbRole.gridx = 2;
		gbc_cbRole.gridy = 5;
		topPanel.add(cbRole, gbc_cbRole);
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.fill = GridBagConstraints.HORIZONTAL;
		gbc_button.insets = new Insets(0, 0, 0, 5);
		gbc_button.gridx = 2;
		gbc_button.gridy = 6;
		topPanel.add(button, gbc_button);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnSave.insets = new Insets(0, 0, 0, 5);
		gbc_btnSave.gridx = 3;
		gbc_btnSave.gridy = 6;
		topPanel.add(btnSave, gbc_btnSave);

		JScrollPane scrollPane = new JScrollPane();
		panel.add(scrollPane, BorderLayout.CENTER);

		table = new JUserTable(this);
		scrollPane.setViewportView(table);
	}

	private JTextField tfUserName;
	private JPasswordField pfPassword;
	private JPasswordField pfOkPassword;
	private JTextField tfName;
	private CustomComboBox cbRole;

	private void add() {
		tfUserName.setEnabled(true);
		clear();
		tfUserName.requestFocus();
	}

	private void clear() {
		tfUserName.setText("");
		pfPassword.setText("");
		pfOkPassword.setText("");
		tfName.setText("");
	}

	private void save() {
		// TODO update table in DB
		if (tfUserName.getText().equals("")
				|| String.valueOf(pfPassword.getPassword()).equals("")
				|| String.valueOf(pfOkPassword.getPassword()).equals("")
				|| tfName.getText().equals("")) {
			// TODO show Dialog
			JOptionPane.showMessageDialog(
					this,
					Tools.get().getValue(
							"LABEL_INPUT_USERNAME_PASSWORD_OKPASSWORD_NAME"),
					Tools.get().getValue("LABEL_PROMPT_TITLE"),
					JOptionPane.ERROR_MESSAGE);
		} else {
			if (!String.valueOf(pfPassword.getPassword()).equals(
					String.valueOf(pfOkPassword.getPassword()))) {
				// TODO show Dialog
				JOptionPane.showMessageDialog(
						this,
						Tools.get().getValue(
								"LABEL_INPUT_PASSWORD_DIFFERENT_OKPASSWORD"),
						Tools.get().getValue("LABEL_PROMPT_TITLE"),
						JOptionPane.ERROR_MESSAGE);

			} else {
//				User user = new User();
//				UserService us = (UserService) BeanFactory
//						.getBean("userService");
//				// TODO is the user exist ?
//				fillUser(user);
//				User existedUser = us.gettUserByUserName(user);
//				if (existedUser != null) {
//					if (JOptionPane.CANCEL_OPTION == JOptionPane
//							.showConfirmDialog(
//									this,
//									Tools.get().getValue("LABEL_UPDATE_PROMPT"),
//									Tools.get().getValue("LABEL_PROMPT_TITLE"),
//									JOptionPane.OK_CANCEL_OPTION)) {
//						return;
//					} else {
//						us.update(user);
//						update();
//					}
//				} else {
//					us.insert(user);
//					update();
//					add();
//				}
//				JOptionPane.showMessageDialog(this,
//						Tools.get().getValue("LABEL_SAVE_OK_PROMPT"), Tools
//								.get().getValue("LABEL_PROMPT_TITLE"),
//						JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}

	private void fillUser(User user) {
		user.setUserName(this.tfUserName.getText());
		user.setPassword(String.valueOf(this.pfPassword.getPassword()));
		user.setName(this.tfName.getText());
		user.setRole(this.cbRole.getSelectedItem().toString());
	}

	public void update() {
		table.update();
	}

	public void showSelectedRow(User user) {
		tfUserName.setEnabled(false);
		tfUserName.setText(user.getUserName());
		pfPassword.setText(user.getPassword());
		pfOkPassword.setText(user.getPassword());
		tfName.setText(user.getName());
		cbRole.setSelectedItem(user.getRole());
	}
}
